package com.sky.admin.config;

//import com.sky.admin.interceptor.JwtTokenAdminInterceptor;
import com.sky.common.json.JacksonObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 * @author toki
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class AdminWebMvcConfiguration extends WebMvcConfigurationSupport {

//    private final JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry 拦截器注册器
     */
//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        log.info("开始注册 管理端 自定义拦截器...");
//        // 管理端拦截器
//        registry.addInterceptor(jwtTokenAdminInterceptor)
//                .addPathPatterns("/admin/**")
//                .excludePathPatterns("/admin/employee/login")
//                .excludePathPatterns("/admin/employee/logout")
//                .excludePathPatterns("/user/**")
//                .excludePathPatterns("/user/shop/status")
//                .excludePathPatterns("/ws/**");
//    }

    /**
     * 通过knife4j生成接口文档
     *
     * @return Docket
     */
    // 注册管理端接口文档
    @Bean
    public Docket docket() {
        log.info("开始通过knife4j生成接口文档...");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("天智餐饮云平台接口文档")
                .version("2.0")
                .description("天智餐饮云平台接口文档")
                .build();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sky.admin.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 设置静态资源映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 扩展SpringMVC框架的消息转换器
     *
     * @param converters SpringMVC的消息转换器列表
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("开始扩展SpringMVC框架的消息转换器...");
        // 创建一个消息转换器对象
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        // 设置消息转换器的对象转Json转换器
        converter.setObjectMapper(new JacksonObjectMapper());
        // 将消息转换器添加到SpringMVC的消息转换器列表的最前面
        converters.add(0, converter);
    }
}
